import {Pipe, PipeTransform} from '@angular/core';

/**
 * 扇形图百分比
 * {{arr|chartRange:0:1}}
 * {{arr|chartRange:1:2}}
 * {{arr|chartRange:2:3}}
 */
@Pipe({name: 'chartRange'})

export class ChartRangePipe implements PipeTransform {
    transform(value: any, ...args: any[]): any {
        if (!value) {
            return value
        }
        let dv = 0;
        let type = 1;
        if (args && args.length > 1) {
            dv = args[0];
            type = args[1];
        }
        let count = 0;
        if (value && value instanceof Array) {
            value.forEach(item => {
                count += +item || 0;
            });
            if (dv >= value.length) {
                return 0;
            }
        } else {
            return value;
        }
        let percent;
        switch (type){
            case 1:
                percent = 45 + (360 * value[dv] / count || 0);
                percent = percent > 225 ? 225 : percent;
                break;
            case 2:
                percent = 45 + (360 * value[dv] / count || 0) - 180;
                percent = percent < 45 ? 45 : percent;
                break;
            case 3:
                percent = value[dv] / count * 100 || 0;
                break;
        }
        return percent;
    }

}
